package com.tpp.comm.util;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;

import javax.crypto.SecretKey;
import java.util.Date;

public class JwtUtil {

    @Value("${jwt.shaKey}")
    private String shaKey;
    private long genTime;//token生成时间,单位毫秒
    @Value("${jwt.expTime}")
    private int expTime;//token的有效时间,分钟

    public long getGenTime() {
        return genTime;
    }

    public void setGenTime(long genTime) {
        this.genTime = genTime;
    }

    public int getExpTime() {
        return expTime;
    }

    public void setExpTime(int expTime) {
        this.expTime = expTime;
    }

    public String createJwt(Integer id){
        genTime=System.currentTimeMillis();
        System.err.println(genTime+","+expTime);
        JwtBuilder builder = Jwts.builder();
        SecretKey secretKey = Keys.hmacShaKeyFor(shaKey.getBytes());
        String jwt = builder.signWith(secretKey, SignatureAlgorithm.HS256)
                .setId(id + "")
                .setIssuedAt(new Date(genTime))
                .setExpiration(new Date(genTime + expTime*1000*60)).compact();
        return jwt;
    }

    public Long getGenTimeByToken(String token){
        JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
        SecretKey secretKey = Keys.hmacShaKeyFor(shaKey.getBytes());
        jwtParserBuilder.setSigningKey(secretKey);
        JwtParser jwtParser = jwtParserBuilder.build();
        try{
            Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token);
            Claims claims = claimsJws.getBody();
            Date date = claims.getIssuedAt();
            return date.getTime();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public Integer getExtpimeByToken(String token){
        JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
        SecretKey secretKey = Keys.hmacShaKeyFor(shaKey.getBytes());
        jwtParserBuilder.setSigningKey(secretKey);
        JwtParser jwtParser = jwtParserBuilder.build();
        try{
            Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token);
            Claims claims = claimsJws.getBody();
            Date date1 = claims.getExpiration();
            Date date2 = claims.getIssuedAt();
            Long millSeconds = date1.getTime()-date2.getTime();
            Long minutes=millSeconds/1000/60;
            return Math.toIntExact(minutes);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    public Integer parseJwt(String token){
        JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
        SecretKey secretKey = Keys.hmacShaKeyFor(shaKey.getBytes());
        jwtParserBuilder.setSigningKey(secretKey);
        JwtParser jwtParser = jwtParserBuilder.build();
        try{
            Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token);
            Claims claims = claimsJws.getBody();
            String idStr = claims.getId();
            Integer id = Integer.valueOf(idStr);
            return id;
        }catch (ExpiredJwtException e){
            String idStr = e.getClaims().getId();
            Integer id = Integer.valueOf(idStr);
            return -id;
        }catch (Exception e){
            return 0;
        }
    }
}
